/**
 *
 */
package com.zhuoyue.zhtj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Maps;
import com.zhuoyue.zhtj.common.ServerResponse;
import com.zhuoyue.zhtj.entity.*;
import com.zhuoyue.zhtj.service.KkcdtjService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.zhtj.service.TmanageclassService;

import java.util.List;
import java.util.Map;

/**
 * 管理班级名单Controller
 * @author hss
 * @version 2017-09-28
 */
@Controller
@RequestMapping(value = "${adminPath}/zhtj/tmanageclass")
public class TmanageclassController extends BaseController {

	@Autowired
	private TmanageclassService tmanageclassService;
	@Autowired
	private KkcdtjService kkcdtjService;
	
	@ModelAttribute
	public Tmanageclass get(@RequestParam(required=false) String id) {
		Tmanageclass entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tmanageclassService.get(id);
		}
		if (entity == null){
			entity = new Tmanageclass();
		}
		return entity;
	}
	
	@RequiresPermissions("zhtj:tmanageclass:view")
	@RequestMapping(value = {"list", ""})
	public String list(Tmanageclass tmanageclass, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Tmanageclass> page = tmanageclassService.findPage(new Page<Tmanageclass>(request, response), tmanageclass); 
		model.addAttribute("page", page);
		return "zhuoyue/zhtj/tmanageclassList";
	}

	@RequiresPermissions("zhtj:tmanageclass:view")
	@RequestMapping(value = "form")
	public String form(Tmanageclass tmanageclass, Model model) {
		model.addAttribute("tmanageclass", tmanageclass);
		return "zhuoyue/zhtj/tmanageclassForm";
	}

	@RequiresPermissions("zhtj:tmanageclass:edit")
	@RequestMapping(value = "save")
	public String save(Tmanageclass tmanageclass, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tmanageclass)){
			return form(tmanageclass, model);
		}
		tmanageclassService.save(tmanageclass);
		addMessage(redirectAttributes, "保存管理班级名单成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/tmanageclass/?repage";
	}
	
	@RequiresPermissions("zhtj:tmanageclass:edit")
	@RequestMapping(value = "delete")
	public String delete(Tmanageclass tmanageclass, RedirectAttributes redirectAttributes) {
		tmanageclassService.delete(tmanageclass);
		addMessage(redirectAttributes, "删除管理班级名单成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/tmanageclass/?repage";
	}

	@RequestMapping(value = "getClassKqjl",method = RequestMethod.POST)
	public String getClassKqjl(Tclass tclass,HttpServletResponse response){
		List<String> list=tmanageclassService.getClassKqjl(tclass);
		Map<String,List<Kkcdtj>> map= Maps.newHashMap();
		if(list!=null&&list.size()>0){
			for(String item:list){
				Kqjl k=new Kqjl();
				k.setNum(item);
				List<Kkcdtj> list1=kkcdtjService.findKKList(k);
				if(list1!=null){
					map.put("kuangke",list1);
				}
				List<Kkcdtj> list2=kkcdtjService.findCDList(k);
				if(list2!=null){
					map.put("chidao",list2);
				}
			}
			return this.renderString(response, ServerResponse.createBySuccess("获取本班信息成功",map));
		}
		return this.renderString(response,ServerResponse.createByErrorMessage("获取本班信息失败"));
	}

	@RequestMapping(value = "getAllPeopleCount",method = RequestMethod.POST)
	public String getAllPeopleCount(Tclass tclass,HttpServletResponse response){
		int count=tmanageclassService.getAllPeopleCount(tclass);
		if(count!=0){
			return this.renderString(response,ServerResponse.createBySuccess("totalpeople",count));
		}
		return this.renderString(response,ServerResponse.createByErrorMessage("本班尚未人员"));
	}

//	@RequestMapping(value = "getCourseByDate")
//	public String getCourseByDate(,HttpServletResponse response){
//
//	}

}